//
//  DFNetWork.m
//  DownGuys
//
//  Created by zhanghongwei on 1/12/16.
//  Copyright © 2016年 down-guys. All rights reserved.
//

#import "DFNetWork.h"
#import "DFNetWorkMgr.h"
#import "DFLog.h"


//存放网络 IP
NSString *netWork_IP = @"";

@implementation DFNetWork

//网络请求的基地址
+ (NSString*)hostURL
{
    NSString *url = netWork_IP;
    
    if (url && url.length>0) {
        return url;
    }
    
    return url;
}

//初始化网络 IP (程序一开始启动的时候就要调用)
+ (void)registerNetWorkIP:(NSString*)netWorkIP
{
    netWork_IP = netWorkIP;
}

/**
 * 网络请求，包含 “GET” 和 "POST" 方法
 *
 * @param request       网络请求方式
 * @param otherParams   网络请求参数
 * @param url 指定网络请求的地址，一般情况为nil，如果为nil，使用一开始设置的ip地址
 */
+ (void)requestAsynchronous:(DFNetWorkRequest*)request otherParams:(NSDictionary*)otherParams url:(NSString*)url success:(requestSuccessBlock)success failure:(requestFailureBlock)failure
{
    DFParamRequest *params = [[DFParamRequest alloc] init];
    //请求方式 GET or POST
    params.method = request.requestType;
    //拼接 请求地址
    if (!url || url.length==0) {
        params.url = [NSString stringWithFormat:@"%@/%@", [self hostURL], request.requestWebApi];
    }
    else {
        params.url = [NSString stringWithFormat:@"%@/%@", url, request.requestWebApi];
    }
    //过滤之后的参数
    params.dicParams = [request dicParamsIgnoredKeys];
    //是否有额外的参数
    if (otherParams && otherParams.count>0) {
        [params.dicParams setValuesForKeysWithDictionary:otherParams];
    }
    //防在header中的参数
    params.dicHeader = nil;
    //请求失败，继续请求的次数
    params.retryCount = request.retryCount;
    
    // 网络请求
    [self requestAsynchronous:params success:success failure:failure];
}


/*
 *
 *
 *
 *
 */
/*****************************以下为私有方法***********************************/

#pragma mark - private
/**
 * 网络请求，包含 “GET” 和 "POST" 方法
 *
 * @param params     网络请求的参数
 */
+ (void)requestAsynchronous:(DFParamRequest*)params
                    success:(requestSuccessBlock)success
                    failure:(requestFailureBlock)failure
{
    
    //网络请求
    [[DFNetWorkMgr sharedInstance] requestAsynchronous:params success:^(NSInteger statusCode, id respond) {
        
        NSMutableDictionary *dicRespond = [NSJSONSerialization JSONObjectWithData:respond options:NSJSONReadingMutableLeaves error:nil];
        NSString * rawString = [[NSString alloc] initWithData:respond encoding:NSUTF8StringEncoding];
        DLog(@"[Request] %@\n[response]%@", params.url, rawString);
        
        DFNetWorkMsg *message = [DFNetWorkMsg mj_objectWithKeyValues:dicRespond];
        if (!message) {
            message = [[DFNetWorkMsg alloc] init];
            message.ret_msg = rawString;
        }
        
        if ([message.ret_code isEqualToString:@"0"]) {
            if (success) {
                success(dicRespond);
            }
        }
        else {
            if (failure) {
                failure(message);
            }
        }

    } failure:^(NSInteger statusCode, NSString *localizedDescription, NSString *errorStr) {
        DFNetWorkMsg *message = [DFNetWorkMsg mj_objectWithKeyValues:errorStr];
        if (!message) {
            message = [[DFNetWorkMsg alloc] init];
        }
        message.ret_code = [NSString stringWithFormat:@"%ld",(long)statusCode];
        if (!message.ret_msg || message.ret_msg.length==0) {
            message.ret_msg = localizedDescription;
        }
        
        if (failure) {
            failure(message);
        }
    }];
    
    
}


@end



















